//
//  RW_SetupView.m
//  Yule
//
//  Created by 王亮 on 2021/1/22.
//  Copyright © 2021 Yule.dating. All rights reserved.
//

#import "RW_SetupView.h"
#import "RW_SetupItemCollectionViewCell.h"
#import "RW_WebViewController.h"
#import "RW_SelfMeViewController.h"

@interface RW_SetupView ()
@property (nonatomic,strong) WLCollectionView *wlclass_collection_view;
@end
@implementation RW_SetupView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"F6F6F6"];
        [self addSubview:self.wlclass_collection_view];
        [self.wlclass_collection_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
        }];
    }
    return self;
}
-(void)setViewModel:(RW_SetupViewModel *)viewModel{
    _viewModel = viewModel;
}
-(void)initData{
    [self.wlclass_collection_view setAllSection:[self.viewModel getCollectionSection]];
}
#pragma mark --数据加载

#pragma mark --代理
//点击cell的响应
- (void)wl_collectionView:(UICollectionView *)collectionView
               layerModel:(WLBaseCollectionViewLayerModel *)model
             didIndexPath:(NSIndexPath *)indexPath{
    WLBaseCollectionViewLayerRow *row = [model getCellModel:indexPath];
    if ([row.data isKindOfClass:RW_SetupModel.class]){
        RW_SetupModel *rowModel = (RW_SetupModel *)row.data;
        if (rowModel.type == Setup_Cell_Type_Clean){
            [self cleanCache];
        } else if (rowModel.type == Setup_Cell_Type_About){
            [RW_SelfMeViewController pushSelfMeFromVc:[self viewController_wl]];
        } else if (rowModel.type == Setup_Cell_Type_Protocol){
            [RW_WebViewController pushProtocolFromVc:[self viewController_wl]];
        } else if (rowModel.type == Setup_Cell_Type_Privacy){
            [RW_WebViewController pushPrivacFromVc:[self viewController_wl]];
        } else if (rowModel.type == Setup_Cell_Type_Copyright){
            [RW_WebViewController pushCopyrightFromVc:[self viewController_wl]];
        } else if (rowModel.type == Setup_Cell_Type_Login_Out){
            [self loginOut];
        }
    }
}
//退出登录
-(void)loginOut{
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"确定要退出登录吗?"
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *alertT = [UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击退出登录");
            [RW_UserModel loginOut];
        }];
        UIAlertAction *alertF = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [actionSheet addAction:alertT];
        [actionSheet addAction:alertF];
        [[self viewController_wl] presentViewController:actionSheet animated:YES completion:nil];
}
//清理图片缓存
-(void)cleanCache{
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"确定要清理吗?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *alertT = [UIAlertAction actionWithTitle:@"清理" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
        [self initData];
    }];
    UIAlertAction *alertF = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [actionSheet addAction:alertT];
    [actionSheet addAction:alertF];
    [[self viewController_wl] presentViewController:actionSheet animated:YES completion:nil];
}
#pragma mark -- 懒加载
-(WLCollectionView *)wlclass_collection_view{
    if (!_wlclass_collection_view){
        _wlclass_collection_view = [WLCollectionView initWithVerticalDelegate:self];
    }
    return _wlclass_collection_view;
}

@end
